package com.iwhalecloud.bss.kite.cucc.service.custview.userinfo;

import java.util.*;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.ProdInstQryDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.UserQueryDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.webService.QryIOTOppoDockingSalesRsp;
import com.iwhalecloud.bss.kite.cucc.service.customerOrderInformation.CustomerOrderInfoService;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceBus;

import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.common.logger.api.ZLogger;
import com.ztesoft.common.logger.factory.ZLoggerFactory;

/**
 * @ClassName : UserInfoHelper
 * @Description :
 * @Author : xie.zhengjun
 * @Date : 2021/3/14 16:32
 */
public class UserInfoHelper {
    private static final ZLogger LOGGER = ZLoggerFactory.getLogger(UserInfoHelper.class, KeyConsts.LOG_MODULE);

    public static String getValueFromUserItemInfoMap(Map<String, Map<String, Object>> userItemInfoMap,
        String attrField) {
        if (KiteMapUtils.isNotEmpty(userItemInfoMap)) {
            Map<String, Object> attrMap = userItemInfoMap.get(attrField);
            return KiteMapUtils.getString(attrMap, "ATTR_VALUE");
        }
        return "";
    }

    public static String getNicheName(String nicheCode, String custId) {
        if (KiteStringUtils.isAnyEmpty(nicheCode, custId)) {
            return "";
        }
        InfServiceBus infServiceBus = SpringUtil.getBean(InfServiceBus.class);
        List<QryIOTOppoDockingSalesRsp> iotoOppoList = new ArrayList<>();
        try {
            iotoOppoList = infServiceBus.queryIOTOppoDockingSales(custId);
        }
        catch (BssException e) {
            LOGGER.error(ErrorConsts.INF_INVOKE_ERROR.getErrorCode(), e, "查询商机出错");
        }
        return iotoOppoList.stream().filter(o -> KiteStringUtils.isEqual(o.getOppoNumber(), nicheCode)).findAny()
            .map(QryIOTOppoDockingSalesRsp::getOppoName).orElse(nicheCode);
    }

    /**
     * 查询某个用户的用户列表信息
     *
     * @param custId
     * @param serialNumberVal
     * @param provinceCode
     * @param eparchyCode
     * @return
     */
    public static ProdInstQryDTO getProdInstQryDTO(String custId, String serialNumberVal, String provinceCode,
                                             String eparchyCode) {
        CustomerOrderInfoService customerOrderInfoService = SpringUtil.getBean(CustomerOrderInfoService.class);
        ProdInstQryDTO prodInstQryDTO = null;
        Map<String, String> map = new HashMap<>(5);
        map.put("custId", custId);
        map.put("accNum", serialNumberVal);
        map.put("eparchyCode", eparchyCode);
        map.put("provinceCode", provinceCode);
        UserQueryDTO userQueryDTO = customerOrderInfoService.queryUserList(map);
        if (Objects.nonNull(userQueryDTO)) {
            List<ProdInstQryDTO> userList = userQueryDTO.getUserList().getList();
            if (KiteListUtils.isNotEmpty(userList)) {
                prodInstQryDTO = userList.get(0);
            }
        }
        return prodInstQryDTO;
    }
}
